package com.ksd.api.controller;

import com.ksd.api.beans.SongCaategoryChild;
import com.ksd.api.beans.SongCategoryData;
import com.ksd.api.beans.SongRecommend;
import com.ksd.api.beans.TvDataResponse;
import com.ksd.api.utils.HttpServletUtil;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.enums.UserStatus;
import com.ksd.system.domain.*;
import com.ksd.system.service.*;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
public class GetSongCategory extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(GetSongCategory.class);
    @Autowired
    private ISysTvDataService sysTvDataService;
    @Autowired
    private ISysMusicAlbumService sysMusicAlbumService;
    @Autowired
    private ISysBoardService sysBoardService;


    @RequestMapping(value = "/api/user/vod/getSongCategory", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getTvType(HttpServletRequest httpServletRequest) {
        logger.info("===================指定获取歌曲分类===============");
        HttpServletUtil.getRequestParam(httpServletRequest);

        SysTvData sysTvData = new SysTvData();
        sysTvData.setStatus(UserStatus.OK.getCode());
        sysTvData.setTypeId(Long.valueOf(SysTvType.songDefaultId));
        List<SysTvData> sysTvDataList = sysTvDataService.selectSysTvDataList(sysTvData);
        List<SongCategoryData> response = new ArrayList<>();
        logger.info("所有歌曲种类个数:" + sysTvDataList.size());
        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String serviceIpUrl = sysBoard.getUrl();

        List<SysTvData> resultDatas = new ArrayList<>();
        for (SysTvData tvData : sysTvDataList) {
            SongCategoryData tvDataResponse = new SongCategoryData();
            logger.info("查询歌曲类型:" + tvData.getName());
            tvDataResponse.setCat_id(Integer.valueOf(tvData.getId().toString()));
            tvDataResponse.setParent_id(Integer.valueOf(SysTvType.songDefaultId));
            tvDataResponse.setPos(Integer.valueOf(tvData.getId().toString()));
            tvDataResponse.setCat_name(tvData.getName());

            SysMusicAlbum sysMusicAlbum = new SysMusicAlbum();
            sysMusicAlbum.setFileType(tvData.getId());
            List<SysMusicAlbum> sysMusicAlbums = sysMusicAlbumService.selectSysMusicAlbumList(sysMusicAlbum);
            if (sysMusicAlbums.size() > 0) {
                resultDatas.add(tvData);
            }
        }

        logger.info("可用音乐分类:"+resultDatas.size());
        for (SysTvData tvData : resultDatas) {
            SongCategoryData tvDataResponse = new SongCategoryData();
            logger.info("歌曲类型:" + tvData.getName());
            tvDataResponse.setCat_id(Integer.valueOf(tvData.getId().toString()));
            tvDataResponse.setParent_id(Integer.valueOf(SysTvType.songDefaultId));
            tvDataResponse.setPos(Integer.valueOf(tvData.getId().toString()));
            tvDataResponse.setCat_name(tvData.getName());

            SysMusicAlbum sysMusicAlbum = new SysMusicAlbum();
            sysMusicAlbum.setFileType(tvData.getId());
            List<SysMusicAlbum> sysMusicAlbums = sysMusicAlbumService.selectSysMusicAlbumList(sysMusicAlbum);
            List<SongCaategoryChild> children = new ArrayList<>();
            logger.info("音乐类型:"+tvData.getName()+" ,专辑个数:" + sysMusicAlbums.size());
            List<SongRecommend> recommends = new ArrayList<>();
            Integer[] childId = new Integer[sysMusicAlbums.size()];
            int i = 0;
            for (SysMusicAlbum albumResult : sysMusicAlbums) {
                System.out.println("专辑详情:========专辑名称==="+albumResult.getTitle());
                SongCaategoryChild songCategoryChild = new SongCaategoryChild();
                songCategoryChild.setCat_id(Integer.valueOf(tvData.getId().toString()));
                songCategoryChild.setParent_id(Integer.valueOf(tvData.getId().toString()));
                songCategoryChild.setPos(Integer.valueOf(albumResult.getId().toString()));
                songCategoryChild.setIcon1(serviceIpUrl+albumResult.getImgUrl());
                songCategoryChild.setCat_name(albumResult.getTitle());
                children.add(songCategoryChild);

                childId[i] = Integer.valueOf(albumResult.getId().toString());
                i++;
                SongRecommend songRecommend = new SongRecommend();
                songRecommend.setAutoid(albumResult.getId());
                songRecommend.setTitle(albumResult.getTitle());
                songRecommend.setDirector("");
                songRecommend.setDescription("");
                songRecommend.setIs_recommend("");
                songRecommend.setRecommend_banner("");
                songRecommend.setImg_address(serviceIpUrl+albumResult.getImgUrl());
                songRecommend.setImgAddress(serviceIpUrl+albumResult.getImgUrl());
                songRecommend.setUrl_address(serviceIpUrl+albumResult.getImgUrl());
                songRecommend.setUrlAddress(serviceIpUrl+albumResult.getImgUrl());
                songRecommend.setFile_type("music");

                recommends.add(songRecommend);
            }
            tvDataResponse.setChild(children);
            tvDataResponse.setChild_id(childId);
            tvDataResponse.setRecommend(recommends);
            response.add(tvDataResponse);
        }
        return AjaxResult.success(response);
    }
}